
package metadatabrowser;

import at.fhj.itm.metadata.Column;
import at.fhj.itm.metadata.Constraint;
import at.fhj.util.Pair;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.jface.viewers.IStructuredContentProvider;

/**
 *
 * @author Seuchter
 */
public abstract class AbstractKeyConstraintContentProvider implements IStructuredContentProvider{

    private static final Map<String, String> CONSTRAINT_NAME_MAPPING;

    static
    {
        CONSTRAINT_NAME_MAPPING = new HashMap<String, String>();
        CONSTRAINT_NAME_MAPPING.put(Constraint.CONSTRAINT_TYPE_CHECK, "Check Constraint");
        CONSTRAINT_NAME_MAPPING.put(Constraint.CONSTRAINT_TYPE_FOREIGN, "Foreign Key Constraint");
        CONSTRAINT_NAME_MAPPING.put(Constraint.CONSTRAINT_TYPE_UNIQUE, "Unique Constraint");
        CONSTRAINT_NAME_MAPPING.put(Constraint.CONSTRAINT_TYPE_PRIMARY_KEY, "Primary Key");
    }


    protected String createColumnList(List<? extends Column> columns)
    {
        StringBuilder sb = new StringBuilder();
        
        for(Column c : columns)
        {
            sb.append(c.getName()).append(" ");
        }
        
        
        return sb.toString();
    }
    protected List<Pair<String,String>> createCommonProperties(Constraint con)
    {
        List<Pair<String,String>> properties = new ArrayList<Pair<String,String>>();
        properties.add(new Pair<String, String>("Name:", con.getName()));
        properties.add(new Pair<String, String>("Column(s)",createColumnList(con.getConstraintColumns())));
        String conType = con.getConstraintType();
        properties.add(new Pair<String, String>("Constraint Type:", CONSTRAINT_NAME_MAPPING.get(conType)));
        return properties;
    }

}
